﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoRentalShop.Controller;
using VideoRentalShop.Model;
using VideoRentalShop.View.Templates;
using System.Windows;
using System.Windows.Data;

namespace VideoRentalShop.View.Windows.MovieList
{
    public class MovieListPanel : ListPanel
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;
        private SearchPanel _panSearch;
        private EditPanel _panEdit;
        private AdditionalPanel _panImage;


        public MovieListPanel()
        {
            _panSearch = new SearchPanel();
            _panEdit = new EditPanel();
            _panImage = new AdditionalPanel();
            AddSearchPanel(_panSearch);
            AddEditPanel(_panEdit);
            AddAdditionalPanel(_panImage, new GridLength(0.35d, GridUnitType.Star));
            CreateListColumns();
        }


        protected override void Refresh()
        {
            _panImage.LoadImage(null);
            _panSearch.Clear();
        }

        private void CreateListColumns()
        {
            AddListColumn("Tytuł", "Title");
            AddListColumn("Oryginalny tytuł", "OriginalTitle");
            AddListColumn("Ilość", "Quantity");
            AddListColumn("Dostępne", "Avaiable");
            AddListColumn("Od lat", "MinAge");
            AddListColumn("Data premiery", new Binding { Path = new PropertyPath("ReleaseDate"), StringFormat = "{0:dd.MM.yyyy}" });
            AddListColumn("Państwo", "OriginCountry");
            AddListColumn("Reżyser", "Director");
            AddListColumn("Opis", "Description");
        }
        
        protected override void ClearClicked()
        {
            _panSearch.Clear();
        }

        protected override void SearchClicked()
        {
            var results = _panSearch.GetResults();
            ShowResults(results);
        }
        
        private void ShowResults(IEnumerable<Movie> results)
        {
            var list = new List<dynamic>();

            foreach (var result in results)
            {
                list.Add(new
                {
                    Item = result,
                    Title = result.Title,
                    OriginalTitle = result.OriginalTitle,
                    Quantity = result.Quantity,
                    Avaiable = result.Avaiable,
                    MinAge = result.MinAge,
                    ReleaseDate = result.ReleaseDate,
                    OriginCountry = result.OriginCountry,
                    Director = result.Director,
                    Description = result.Description
                });
            }

            ShowListResults(list);
        }

        
        protected override void EditClicked()
        {
            if (lstResults.SelectedIndex > -1)
            {
                var completed = _panEdit.Edit();

                if (completed)
                {
                    RefreshList();
                }
            }
            else
            {
                _controller.ShowMessage("Wymagane jest uprzednie wybranie filmu z listy.", "Błąd", MessageType.Error);
            }
        }

        protected override void AddClicked()
        {
            var completed = _panEdit.Add();

            if (completed)
            {
                RefreshList();
            }
        }

        protected override void RemoveClicked()
        {
            var count = lstResults.SelectedItems.Count;
            var completed = 0;

            foreach (dynamic lstItem in lstResults.SelectedItems)
            {
                var item = (Movie)lstItem.Item;

                var notReturnedCount = item.Orders.Where(x => x.ReturnDate == null).Count();

                if (notReturnedCount > 0)
                {
                    var result = MessageBox.Show("Kopie tego filmu są aktualnie wypożyczone przez klienta. Czy chcesz go usunąć mimo to?", "Ostrzeżenie", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                    if (result != MessageBoxResult.Yes)
                    {
                        return;
                    }
                }

                if (_controller.Data.Movie.Remove(item))
                {
                    ++completed;
                }
            }

            if (completed != count)
            {
                _controller.ShowMessage("Podczas usuwania filmów wystąpił błąd", "Błąd");
            }
            
            if (completed > 0)
            {
                RefreshList();
            }
        }
        
        private void RefreshList()
        {
            SearchClicked();
        }
        
        protected override void SelectionChanged()
        {
            var itemsCount = lstResults.SelectedItems.Count;

            if (itemsCount > 0)
            {
                dynamic lstItem = lstResults.SelectedItem;
                var item = lstItem.Item as Movie;

                if (item != null)
                {
                    _panEdit.LoadItem(item);
                    _panImage.LoadImage(item);
                }
            }
        }
    }
}
